﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _plants_vs_zombies
{
    public partial class FormMain : Form
    {
        private Game _game;
        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化游戏类
            _game = new Game();
            _game.gameChanged += _game_gameChanged;
            _game.gameOver += _game_gameOver;
        }

        private void _game_gameOver()
        {
            if (_game.IsGameOver()== 3)
            {
                MessageBox.Show("僵尸吃掉了你的脑袋！");
                //_game.k = 0;
                //重绘游戏
                _game = new Game();

            }
            if (_game.IsGameOver() == 2 &&_game.l==1)
            {
                MessageBox.Show("宇智波鼬恭喜你游戏胜利！");
                _game.l = 0;
                _game.k = 0;
                //重绘游戏
                _game = new Game();
            }
        }

        private void _game_gameChanged()
        {
            if (_game != null) //游戏已经初始化了
            {
                GameArea.Invalidate();
                //if (_game.IsGameOver(z))
                //{
                //    //游戏结束
                //    MessageBox.Show("Game Over");
                //    //重置游戏
                //    _game.gameChanged += _game_gameChanged;
                //}
            }
        }

        private void userControlNoBlink1_Load(object sender, EventArgs e)
        {

        }

        private void GameArea_Paint(object sender, PaintEventArgs e)
        {
            if (_game != null) //游戏已经初始化了
            {
                _game.Draw(e.Graphics, this.GameArea.Size);
            }
        }

        private void GameArea_MouseDown(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button==MouseButtons.Left)
            {
                //调用game的鼠标点击处理程序
                _game.MouseDown(e.Location, this.GameArea.Size);
                //重绘游戏区域
                this.GameArea.Invalidate();
            }
        }

        private void GameArea_MouseMove(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button == MouseButtons.Left)
            {
                //调用game的鼠标点击处理程序
                _game.MouseMove(e.Location, this.GameArea.Size);
                //重绘游戏区域
                this.GameArea.Invalidate();
            }
        }

        private void GameArea_MouseUp(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button == MouseButtons.Left)
            {
                //调用game的鼠标点击处理程序
                _game.MouseUp(e.Location, this.GameArea.Size);
                //重绘游戏区域
                this.GameArea.Invalidate();
            }
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            //重置游戏
            _game = new Game();
            _game.gameChanged += _game_gameChanged1;            
        }

        private void _game_gameChanged1()
        {
            this.GameArea.Invalidate();
        }

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutme frm= new FrmAboutme();
            frm.ShowDialog();
        }
    }
}
